package com.beiming.kotlindemo

import java.lang.StringBuilder

/**
 * Created by lingchen on 2020/5/5. 21:41
 * mail:lingchen52@foxmail.com
 */
class StandardMethod {

    companion object {

        @JvmStatic
        fun test() {

        }
    }

    fun main() {
        //在任意位置都可以直接调用顶层方法
        doSomething()
    }

    val list = listOf("Apple", "Banana", "Orange", "Pear", "Grape", "Watermelon")

    private fun test1() {
        val builder = StringBuilder()
        builder.append("Start eating fruits.\n")
        for (fruit in list) {
            builder.append(fruit).append("\n")
        }
        builder.append("Ate all fruits.\n")
        val result = builder.toString()
        println(result)
    }

    /**
     * 标准函数with  返回最后一行代码的返回值
     */
    private fun test2() {
        val result = with(StringBuilder()) {
            append("Start eating fruits.\n")
            for (fruit in list) {
                append(fruit).append("\n")
            }
            append("Ate all fruits.\n")
            toString()
        }
        println(result)
    }

    /**
     * 标准函数run  返回最后一行代码的返回值
     */
    private fun test3() {
        val result = StringBuilder().run {
            append("Start eating fruits.\n")
            for (fruit in list) {
                append(fruit).append("\n")
            }
            append("Ate all fruits.\n")
            toString()
        }
        println(result)
    }

    /**
     * 标准函数apply  返回调用对象本身
     */
    private fun test4() {
        val result = StringBuilder().apply {
            append("Start eating fruits.\n")
            for (fruit in list) {
                append(fruit).append("\n")
            }
            append("Ate all fruits.\n")
        }
        println(result.toString())
    }
}